<?php
/**
 * Created by IntelliJ IDEA.
 * User: Neo
 * Date: 2017/4/10
 * Time: 10:39
 */

namespace Lib;


class Status
{
    private $Model;
    private $id;

    protected $_map = array(
        'start' => array(1, '启用'),
        'stop' => array(0, '停用'),
        'del' => array(-1, '删除')
    );
    
    
    public function __construct($Model, $id, $action)
    {
        $this->Model = $Model;
        $this->id=is_numeric($id)?[$id]:$id;
        if ($action==='remove'){
            $this->_remove();
        }else{
            $this->_update($action);
        }
    }

    private function _update($action)
    {
        if ($this->_map[$action]) {
            $data['status'] = $this->_map[$action][0];
            $des = $this->_map[$action][1];
            $succeed = 0;
            foreach ($this->id as $i => $item) {
                $data['id'] = $item;
                if ($this->Model->save($data) !== false) {
                    $succeed++;
                }
            }
            show(1, '共选择 ' . sizeof($this->id) . ' 条记录，成功' . $des . ' ' . $succeed . ' 条');
        } else {
            show(1, '非法操作');
        }
    }
    private function _remove()
    {
        $succeed = 0;
        foreach ($this->id as $i => $item) {
            if ($this->Model->delete($item) != false) {
                $succeed++;
            }
        }
        show(1, '共选择 ' . sizeof($this->id) . ' 条记录，成功删除' . $succeed . ' 条');
    }
}